package com.example.goodpet.common;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.goodpet.R;
import com.example.goodpet.utils.UIUtils;

public abstract class IsLoginPage extends FrameLayout {
    //1.定义两种状态
    public static final int STATE_UN_LOGIN=1;
    public static final int STATE_loading=2;
    public static final int STATE_ERROR=3;
    public static final int STATE_EMPTY=4;
    public static final int STATE_SUCCESS=5;

    //2.定义当前状态
    private int stateCurrent=STATE_UN_LOGIN;//默认值

    //3.定义两种种不同的界面
    private View viewUnLogin;
    private View viewLoading;
    private View viewError;
    private View viewEmpty;
    private View viewSuccess;
    private LayoutParams params;


    public LinearLayout layoutUnLogin;
    public IsLoginPage(@NonNull Context context) {
        super(context);
        init();
    }


    public IsLoginPage(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public IsLoginPage(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void setStateCurrent(int stateCurrent) {
        this.stateCurrent = stateCurrent;
        showSafePage();
    }

    private void init() {
        params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (viewUnLogin==null){
            viewUnLogin= UIUtils.getView(R.layout.page_un_login);
            layoutUnLogin=viewUnLogin.findViewById(R.id.layoutUnLogin);
            addView(viewUnLogin,params);
        }
        if (viewLoading==null){
            viewLoading= UIUtils.getView(R.layout.page_loading);
            addView(viewLoading,params);
        }
        if (viewError==null){
            viewError= UIUtils.getView(R.layout.page_error);
            addView(viewError,params);
        }
        if (viewEmpty==null){
            viewEmpty= UIUtils.getView(R.layout.page_empty);
            addView(viewEmpty,params);
        }
        showSafePage();
        
    }

    private void showSafePage() {
        UIUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showPage();
            }
        });
    }

    private void showPage() {
        viewUnLogin.setVisibility(stateCurrent==STATE_UN_LOGIN?VISIBLE:INVISIBLE);
        viewLoading.setVisibility(stateCurrent==STATE_loading?View.VISIBLE:View.INVISIBLE);
        viewError.setVisibility(stateCurrent==STATE_ERROR?View.VISIBLE:View.INVISIBLE);
        viewEmpty.setVisibility(stateCurrent==STATE_EMPTY?View.VISIBLE:View.INVISIBLE);
        if (viewSuccess==null){
            viewSuccess=UIUtils.getView(getLayoutXMLId());
            addView(viewSuccess,params);
        }
        viewSuccess.setVisibility(stateCurrent==STATE_SUCCESS?View.VISIBLE:View.INVISIBLE);
    }

    public abstract int getLayoutXMLId() ;
}
